import {
  StockNode,
  DisableRes,
  StockNodeGateway,
  patchStockNodeDto,
  deleteStockNodeDto,
  putStockNodeChildDto,
  StockNodeInterfaceWithGrant,
  putStockNodeDto,
  PutMutiRes,
} from "qqlx-core";

import { StockNodeSchema } from "@/river/ton-stock-node";
import { ViewBase, ViewBaseAction } from "@view/view-base";

export class ViewStockNode extends ViewBase<StockNode> implements ViewBaseAction<StockNode>, StockNodeInterfaceWithGrant<{}> {
  private static instance: ViewStockNode;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewStockNode();
    }

    if (!this.instance) this.instance = new ViewStockNode();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): StockNode {
    return new StockNodeSchema();
  }

  async put(dto: putStockNodeDto) {
    this.pending();
    return this.$request.put<putStockNodeDto, PutMutiRes<StockNode>>(StockNodeGateway.path, dto);
  }

  async putChildSync(input: putStockNodeChildDto) {
    this.pending();
    const path = `${StockNodeGateway.path}/child`;
    const dto = { ...input, schemaList: input.schemaList ?? this.data.schemaList };
    return this.$request.put<putStockNodeChildDto, PutMutiRes<StockNode>>(path, dto);
  }

  async patch(dto: patchStockNodeDto) {
    this.pending();
    return this.$request.patch<patchStockNodeDto, null>(StockNodeGateway.path, dto);
  }

  async delete(dto: deleteStockNodeDto) {
    this.pending();
    const res = await this.$request.delete<deleteStockNodeDto, DisableRes>(StockNodeGateway.path, dto);
    return res;
  }

  async getTask() {
    this.pending();
    return this.$request.getByPut<null, { count: number }>(`${StockNodeGateway.path}/task`);
  }
}
